﻿'
' Copyright (c) 2010-2012, MatthiWare
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of this software and associated documentation files (the "Software"),
' to deal in the Software without restriction, including without limitation
' the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software. 
' You shall include 'MatthiWare' in the credits/about section of the program
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'

Imports System.Threading

Public Class BlockUpdater

    Public Sub DoThreadedBlockUpdate(b As Block, e As Core.Base.GameCoreEventArgs)
        For Each sb As Block In Block.GetSurroundingBlocks(b, e)
            RequestBlockCheck(sb, e)
        Next
    End Sub

    Public Sub DoThreadedMakeAir(b As Block(), e As Core.Base.GameCoreEventArgs)
        For Each sb As Block In b
            If (Not IsNothing(sb)) Then MakeAir(sb, e)
        Next

    End Sub

    Public Sub MakeAir(b As Block, e As Core.Base.GameCoreEventArgs)
        If (Not b.Info.Name = "Air") Then
            b.Info = Tile.Air
            DoThreadedBlockUpdate(b, e)
        End If
    End Sub

    Public Sub RequestBlockCheck(b As Block, ee As Core.Base.GameCoreEventArgs)
        Dim world As World = ee.Core.Services.GetService(GetType(World))
        Dim oreFlag As Boolean = False
        Dim stoneFlag As Boolean = False
        Dim brightFlag As Boolean = False

        If (Not IsNothing(b)) Then
            If (b.Info.Name.Contains("Ore")) Then oreFlag = True

            If (b.Info.Name = "Stone") Then stoneFlag = True

            If (b.Info.Name = "Air") Then
                Return
            End If
        Else
            Return
        End If

        Dim block1 As Block = Nothing
        Dim block2 As Block = Nothing
        Dim block3 As Block = Nothing
        Dim block4 As Block = Nothing

        If ((b.Y - 1) >= 0) Then block1 = world.level(b.X, b.Y - 1)
        If ((b.X + 1) <= world.WorldWidth - 1) Then block2 = world.level(b.X + 1, b.Y)
        If ((b.Y + 1) <= world.WorldHeight - 1) Then block3 = world.level(b.X, b.Y + 1)
        If ((b.X - 1) >= 0) Then block4 = world.level(b.X - 1, b.Y)

        Dim n As Boolean = False
        Dim e As Boolean = False
        Dim s As Boolean = False
        Dim w As Boolean = False

        If (Not IsNothing(block1)) Then
            If (block1.Info.Name = "Air") Then n = True
            If ((Not oreFlag) And (block1.Info.Name.Contains("Ore"))) Then n = True
            If (oreFlag And (Not block1.Info.Name.Contains("Ore"))) Then n = True
            If ((Not stoneFlag) And (block1.Info.Name.Contains("Stone"))) Then n = True
            If (stoneFlag And (Not block1.Info.Name.Contains("Stone"))) Then n = True
        Else : n = True
        End If

        If (Not IsNothing(block2)) Then
            If (block2.Info.Name = "Air") Then e = True
            If ((Not oreFlag) And (block2.Info.Name.Contains("Ore"))) Then e = True
            If (oreFlag And (Not block2.Info.Name.Contains("Ore"))) Then e = True
            If ((Not stoneFlag) And (block2.Info.Name.Contains("Stone"))) Then e = True
            If (stoneFlag And (Not block2.Info.Name.Contains("Stone"))) Then e = True
        Else : e = True
        End If

        If (Not IsNothing(block3)) Then
            If (block3.Info.Name = "Air") Then s = True
            If ((Not oreFlag) And (block3.Info.Name.Contains("Ore"))) Then s = True
            If (oreFlag And (Not block3.Info.Name.Contains("Ore"))) Then s = True
            If ((Not stoneFlag) And (block3.Info.Name.Contains("Stone"))) Then s = True
            If (stoneFlag And (Not block3.Info.Name.Contains("Stone"))) Then s = True
        Else : s = True
        End If

        If (Not IsNothing(block4)) Then
            If (block4.Info.Name = "Air") Then w = True
            If ((Not oreFlag) And (block4.Info.Name.Contains("Ore"))) Then w = True
            If (oreFlag And (Not block4.Info.Name.Contains("Ore"))) Then w = True
            If ((Not stoneFlag) And (block4.Info.Name.Contains("Stone"))) Then w = True
            If (stoneFlag And (Not block4.Info.Name.Contains("Stone"))) Then w = True
        Else : w = True
        End If

        Dim r As New Random()

        'allemaal
        If (n And e And s And w) Then
            b.Info.Id(1) = 9 * 3

            ' 3 zijden
        ElseIf (n And e And s) Then
            b.Info.Id(1) = r.Next((13 * 3), (13 * 3) + 2)
        ElseIf (n And e And w) Then
            b.Info.Id(1) = r.Next((10 * 3), (10 * 3) + 2)
        ElseIf (e And s And w) Then
            b.Info.Id(1) = r.Next((12 * 3), (12 * 3) + 2)
        ElseIf (s And w And n) Then
            b.Info.Id(1) = r.Next((11 * 3), (11 * 3) + 2)

            ' 2 zijden
        ElseIf (n And e) Then
            b.Info.Id(1) = r.Next((6 * 3), (6 * 3) + 2)
        ElseIf (n And w) Then
            b.Info.Id(1) = r.Next((5 * 3), (5 * 3) + 2)
        ElseIf (e And s) Then
            b.Info.Id(1) = r.Next((8 * 3), (8 * 3) + 2)
        ElseIf (w And s) Then
            b.Info.Id(1) = r.Next((7 * 3), (7 * 3) + 2)

            ' 1 zijde
        ElseIf (n) Then
            b.Info.Id(1) = r.Next((3 * 3), (3 * 3) + 2)
        ElseIf (e) Then
            b.Info.Id(1) = r.Next((2 * 3), (2 * 3) + 2)
        ElseIf (s) Then
            b.Info.Id(1) = r.Next((4 * 3), (4 * 3) + 2)
        ElseIf (w) Then
            b.Info.Id(1) = r.Next((1 * 3), (1 * 3) + 2)
        Else
            b.Info.Id(1) = 2
        End If

        b.Init()

    End Sub

    'Public Sub RequestBlockCheck(x As Int32, y As Int32)
    '    Dim world As World = world.CurrentWorld
    '    RequestBlockCheck(world.level(x, y))

    'End Sub

End Class
